//
//  LLT_VideoDetailTableViewController_LXH.m
//  CoderLLT
//
//  Created by lanou3g on 15/12/15.
//  Copyright © 2015年 刘晓萌. All rights reserved.
//

#import "LLT_VideoDetailTableViewController_LXH.h"
#import "LLT_VideoModel.h"
#import "AFNetworking.h"
#import "LLT_VideoTableViewCell_TM.h"
#import "LLT_VideoDetailTableViewCell_LXH.h"
#import "LLT_PersonModel.h"
#import "LLT_VideoViewController_LXH.h"
#import "LLT_LiveSmallTableViewCell_LXH.h"
#import "LLT_PlayController_TM.h"
#import "LLT_PlayViewController_LXM.h"
#import "MJRefresh.h"



@interface LLT_VideoDetailTableViewController_LXH ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *videoArray;

//刷新
@property (nonatomic, strong) MJRefreshNormalHeader *refreshHeader;

@property (nonatomic, strong) MJRefreshBackNormalFooter *refreshFooter;


@end

@implementation LLT_VideoDetailTableViewController_LXH

/**
 * 懒加载
 */
-(NSMutableArray *)videoArray{
    
    if (!_videoArray) {
        
        _videoArray = [NSMutableArray new];
        
    }
    return _videoArray;
}

/**
 * 数组里数据的个数
 */
-(NSInteger)countOfvideoArray{
    
    return self.videoArray.count;
    
}


- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self makeData];
    
    [self makeTableView];
    
    [self makeBackground];
    
 
}

#pragma mark --- 设置页面

-(void)makeBackground{
    
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"bgBlack"] forBarMetrics:UIBarMetricsDefault];
    self.navigationItem.title = @"视频";
    //设置中间titile的字体颜色
    [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    UIBarButtonItem *left = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-yinlefanhui"] style:UIBarButtonItemStyleDone target:self action:@selector(leftAction:)];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = left;
    
}



/**
 * 左按钮返回
 */
-(void)leftAction:(UIBarButtonItem*)sender{
    DissMissBack;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}

/**
 * 获取数据
 */
-(void)makeData{
    
    
    NSString *s = kVideoURL(self.dm_uid);
    AFHTTPRequestOperationManager *myManager = [AFHTTPRequestOperationManager manager];
    myManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
    [myManager GET:s parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSDictionary *dictionary = responseObject;
        NSArray *array = dictionary[@"result"][@"video_list_by_time"];
        for (NSDictionary *dict in array) {
            LLT_VideoModel *m = [[LLT_VideoModel alloc]init];
            [m setValuesForKeysWithDictionary:dict];
            [self.videoArray addObject:m];
        }
        [self.tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        //NSLog(@"%@",error);
    }];
     
}

/**
 * 设置tableView
 */
-(void)makeTableView{
    
    [self.tableView addSubview:self.refreshHeader];
    [self.tableView addSubview:self.refreshFooter];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView  registerNib:[UINib nibWithNibName:@"LLT_VideoTableViewCell_TM" bundle:nil] forCellReuseIdentifier:@"BigCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"LLT_VideoDetailTableViewCell_LXH" bundle:nil] forCellReuseIdentifier:@"SmallCell"];
}



#pragma mark - set MJRefresh

/**
 * 下拉刷新
 */
-(MJRefreshNormalHeader *)refreshHeader{
    if (!_refreshHeader) {
        _refreshHeader = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.videoArray = [NSMutableArray new];
            [self makeData];
            [self.tableView reloadData];
            [_refreshHeader endRefreshing];
        }];
    }
    return _refreshHeader;
}

/**
 *上拉加载
 */
-(MJRefreshBackNormalFooter *)refreshFooter{
    if (!_refreshFooter) {
        _refreshFooter = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            [self makeData];
            [_refreshFooter endRefreshing];
        }];
    }
    return _refreshFooter;
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.videoArray.count;
}
/**
 * 设置每个cell 的高度
 */
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (!indexPath.row) {
        return 200;
    }else{
        return 90;
    }
    
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        //直播的大cell
        return [self makeLiveBigCellWithTableView:tableView AtIndexPath:indexPath];
    }else{
        //直播的小cell
        return [self makeLiveSmallCellWithTableView:tableView AtIndexPath:indexPath];
    }
}

/**
 * 创建小cell
 */
-(LLT_VideoTableViewCell_TM *)makeLiveBigCellWithTableView:(UITableView *)tableView AtIndexPath:(NSIndexPath *)indexPath{
    
    LLT_VideoTableViewCell_TM *cell = [tableView dequeueReusableCellWithIdentifier:@"BigCell" forIndexPath:indexPath];
    LLT_VideoModel *s = self.videoArray[indexPath.row];
    cell.videoModel = s;
    
    return cell;
}

/**
 * 创建大cell
 */
-(LLT_VideoDetailTableViewCell_LXH *)makeLiveSmallCellWithTableView:(UITableView *)tableView AtIndexPath:(NSIndexPath *)indexPath{
    
    LLT_VideoDetailTableViewCell_LXH *cell = [tableView dequeueReusableCellWithIdentifier:@"SmallCell" forIndexPath:indexPath];
    LLT_VideoModel *s = self.videoArray[indexPath.row];
    cell.videoModel = s;
    return cell;
}

/**
 * 跳转页面
 */
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    LLT_PlayViewController_LXM *playViewController = [MainStoryBoard instantiateViewControllerWithIdentifier:@"playVC"];
    
    LLT_VideoModel *videoModel = self.videoArray[indexPath.row];
    playViewController.videoModel = videoModel;
    
    NSArray *arr = videoModel.segs;
    for (NSDictionary *dic in arr) {
        [videoModel setValuesForKeysWithDictionary:dic];
        [self.videoArray addObject:videoModel];
    }
    
    NSArray *subarr1 = [videoModel.url componentsSeparatedByString:@"vid"];
    NSString *s1 = subarr1[1];
    NSArray *subarr2 = [s1 componentsSeparatedByString:@"&"];
    NSString *s2 = subarr2[0];
    s2 = [s2 stringByReplacingOccurrencesOfString:@"=" withString:@""];
    playViewController.url = s2;
    [self presentViewController:playViewController animated:YES completion:nil];
    
}


/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */

/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
 } else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }
 }
 */

/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
